// pages/withdraw/withdraw.js
import { getWithdrawInfo,updateWithdraw} from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    bank_name:'',
    card_no:'',
    name:'',
    typeList:[],
    countMsg:'',
    nameMsg:'',
    cardMsg:'',
    bankMsg:'',
    info:{}
  },
  initField(e){
    var fieldName=e.currentTarget.id
    switch (fieldName){
      case 'nameMsg':
        this.setData({
          nameMsg: '',
        })
        break;
      case 'cardMsg':
        this.setData({
          cardMsg: '',
        })
        break;
      case 'bankMsg':
        this.setData({
          bankMsg: '',
        })
        break;
      case 'countMsg':
        this.setData({
          countMsg: '',
        })
        break;
    }
  },
  onChange(event) {
    if(event.detail!=this.data.info.type){
      this.setData({
        bank_name:'',
        card_no:'',
        name:'',
      });
    }else{
      this.setData({
        bank_name:this.data.info.bank_name,
        card_no:this.data.info.card_no,
        name:this.data.info.name,
      });
    }
    this.setData({
      type: event.detail,
      countMsg:'',
      nameMsg:'',
      cardMsg:'',
      bankMsg:'',
    });
  },
  formSubmit(e){
    let flag=false
    if(e.detail.value.count!=undefined &&e.detail.value.count==''){
      this.setData({
        countMsg: '请输入账号',
      })
      flag=true
    }
    if(e.detail.value.name!=undefined&&e.detail.value.name==''){
      this.setData({
        nameMsg: '请输入姓名',
      })
      flag=true
    }
    if(e.detail.value.card_no!=undefined&&e.detail.value.card_no==''){
      this.setData({
        cardMsg: '请输入卡号',
      })
      flag=true
    }
    if(e.detail.value.bank_name!=undefined&&e.detail.value.bank_name==''){
      this.setData({
        bankMsg: '请输入银行',
      })
      flag=true
    }
    if(flag){
      return;
    }
    const params={
      type: this.data.type,
      bank_name:e.detail.value.bank_name||'',
      card_no:e.detail.value.card_no||e.detail.value.count,
      name:e.detail.value.name,
    }
    updateWithdraw(params).then(res=>{
      let that=this
      wx.showToast({
        icon:'success',
        title: '保存成功',
        success:function(){
          wx.navigateBack({
            data:1
          })     
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getData(){
    getWithdrawInfo().then(res=>{
      this.setData({
        type: res.data.type,
        bank_name:res.data.bank_name,
        card_no:res.data.card_no,
        name:res.data.name,
        typeList:res.data.condition.type,
        info:res.data
      })
    })
  },
  onLoad(options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})